package com.fjwt.gz.pay.mch.mchinfo;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.pay.PayKit;
import com.fjwt.gz.zhyx.AESUtils;
import com.fjwt.gz.zhyx.SignUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class MchInfoSyncService {

    /**
     * 获取收单平台的商户信息
     **/
    public MchInfoResponseData getMchInfos(JSONObject jsonObject) {
        log.info("同步商户信息未加密请求参数：{}",JSONUtil.toJsonStr(jsonObject));

        String realNameAuthUrl = PayKit.MCH_INFO_URL;
        String encryptStr = AESUtils.getInstance().encrypt(JSONUtil.toJsonStr(jsonObject));
        long timeStamp = System.currentTimeMillis();
        String authSign = SignUtils.getEncryptSign(encryptStr, timeStamp);

        log.info("同步商户信息请求地址：{}, 请求参数：{}, 加密串：{}", realNameAuthUrl, encryptStr, authSign);
        try {

            HttpResponse response = HttpRequest.post(realNameAuthUrl)
                    .header("Content-Type", MediaType.TEXT_PLAIN_VALUE)
                    .header("sign-type", SignUtils.SIGN_TYPE)
                    .header("auth-apikey", SignUtils.API_KEY)
                    .header("auth-sign", authSign)
                    .header("auth-timestamp", String.valueOf(timeStamp))
                    .header("connevtion","Keep-Alive")
                    .header("Referer","https://jrstyy.post183.net")
                    .timeout(60000)
                    .body(encryptStr)
                    .execute();
            log.info("同步商户信息,response:{}",JSONUtil.toJsonStr(response));
            String body = response.body();
            log.info("同步商户信息返回的数据是：{}", JSONUtil.toJsonStr(body));
            MchInfoResponse payResponse = JSONObject.parseObject(body, MchInfoResponse.class);
            String code = payResponse.getCode();
            if ("0".equals(code)) {
                MchInfoResponseData data = payResponse.getData();
                return data;
            } else {
                throw new BizException(payResponse.getMsg());
            }

        } catch (Exception e) { //出现异常，直接退出，避免死循环。
            log.error("error", e);
        }
        return null;
    }

    /**认证C端消费者的商户身份**/
    public String checkFansIsMch(JSONObject jsonObject){
        log.info("认证C端消费者的商户身份未加密请求参数：{}",JSONUtil.toJsonStr(jsonObject));

        String checkFansIsMchUrl = PayKit.CHECK_FANS_IS_MCH_URL;
        String encryptStr = AESUtils.getInstance().encrypt(JSONUtil.toJsonStr(jsonObject));
        long timeStamp = System.currentTimeMillis();
        String authSign = SignUtils.getEncryptSign(encryptStr, timeStamp);

        log.info("认证C端消费者的商户身份请求地址：{}, 请求参数：{}, 加密串：{}", checkFansIsMchUrl, encryptStr, authSign);
        try {

            HttpResponse response = HttpRequest.post(checkFansIsMchUrl)
                    .header("Content-Type", MediaType.TEXT_PLAIN_VALUE)
                    .header("sign-type", SignUtils.SIGN_TYPE)
                    .header("auth-apikey", SignUtils.API_KEY)
                    .header("auth-sign", authSign)
                    .header("auth-timestamp", String.valueOf(timeStamp))
                    .header("Referer","https://jrstyy.post183.net")
                    .timeout(20000)
                    .body(encryptStr)
                    .execute();
            int status = response.getStatus();
            if(status!=200){
                throw new BizException("认证C端消费者的商户身份错误");
            }
            String body = response.body();
            log.info("认证C端消费者的商户身份返回的数据是：{}", JSONUtil.toJsonStr(body));
            JSONObject respJson = JSONObject.parseObject(body);
            String code = respJson.getString("code");
            if ("0".equals(code)) {
                String data = respJson.getString("data");

                return data;
            } else {
                throw new BizException(respJson.getString("msg"));
            }

        } catch (BizException e) { //出现异常，直接退出，避免死循环。
         throw e;
        }catch (Exception e) { //出现异常，直接退出，避免死循环。
            log.error("error", e);
        }
        return "false";

    }
}

